<?php
FLEA::loadClass('Controller_Base');

class Controller_Invention extends Controller_Base
{
	function	Controller_Invention()
	{
		$this->module	=	'Invention';
		$this->_modelInvention	=&	FLEA::getSingleton('Model_Invention');
		$this->_modelAction		=&	FLEA::getSingleton('Model_Action');
		$this->_modelDescription=&	FLEA::getSingleton('Model_Description');
	}
	/**
	 * viewInventions接口，显示所有与之有关联的inventions
	 *
	 */
	function	actionGetInventions()
	{
		$role	=	$_POST['Role'];
		$userID	=	$_POST['UserID'];

		$role	=	$role==''?0:$role;
		$userID	=	$userID==''?0:$userID;
		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
		$inventions	=	$_modelInvention->findAllInventions($userID,$role);

		if(is_array($inventions))
		{
			$this->purse($inventions,'inventions',0);
		}
	}

	function	actionGetInvention()
	{
		$inventionID	=	$_POST['InventionID'];

		$_modelInvention=&	FLEA::getSingleton('Model_Invention');
		$invention		=	$_modelInvention->findInvention($inventionID);

		if(is_array($invention))
		{
			$this->purse($invention,'invention',0);
		}
	}

	function	actionGetActions()
	{
		$inventionID	=	$_POST['InventionID'];
		$inventionID	=	$inventionID==''?0:$inventionID;
		$_modelAction	=&	FLEA::getSingleton('Model_Action');
		$actions		=	$_modelAction->findActions($inventionID);

		$officeActions		=	$_modelAction->findOfficeActions($inventionID);

		print '<actions><actions>';
		for($i=0; $i<count($actions) ; $i++)
		{
			print '<action
				label="'.$GLOBALS['ActionType'][$actions[$i]['Type']].'   [ Date:'.$actions[$i]['Date'] .']"
				ActionID="'.$actions[$i]['ActionID'].'"
				
				Type="'.$actions[$i]['Type'].'"
				Date="'.$actions[$i]['Date'].'"
				Description="'.$actions[$i]['Description'].'"
				DueDate="'.$actions[$i]['DueDate'].'">';
			for($j=0; $j<count($actions[$i]['descriptions']); $j++)
			{


				print '<File label="';
				print substr($actions[$i]['descriptions'][$j]['Drawing0'],8,strlen($actions[$i]['descriptions'][$j]['Drawing0'])-8);
				print '"></File>';
			}
			print '</action>';
		}
		print '</actions>';
		if(is_array($officeActions))
		{
			$this->purse($officeActions,'officeActions',0);
		}
		print '</actions>';
	}

	function	actionGetResponseActions()
	{
		$actionID		=	$_POST['ActionID'];
		$actionID		=	$actionID==''?0:$actionID;
		$_modelAction	=&	FLEA::getSingleton('Model_Action');
		$actions		=	$_modelAction->findResponseActions($actionID);

		print '<actions>';
		for($i=0; $i<count($actions) ; $i++)
		{
			print '<action
				label="'.$GLOBALS['officeResponseType'][$actions[$i]['OfficeActionType']].'   [ Date:'.$actions[$i]['Date'] .']"
				ActionID="'.$actions[$i]['ActionID'].'"
				ResponseDate="'.$actions[$i]['ResponseDate'].'"
				Type="'.$GLOBALS['officeResponseType'][$actions[$i]['OfficeActionType']].'"
				Date="'.$actions[$i]['Date'].'"
				Cost="'.$actions[$i]['Cost'].'"
				Description="'.$actions[$i]['Description'].'"
				DueDate="'.$actions[$i]['DueDate'].'">';
			for($j=0; $j<count($actions[$i]['descriptions']); $j++)
			{


				print '<File label="';
				print substr($actions[$i]['descriptions'][$j]['Drawing0'],8,strlen($actions[$i]['descriptions'][$j]['Drawing0'])-8);
				print '"></File>';
			}
			print '</action>';
		}
		print '</actions>';
	}

	function	actionGetBidInventions()
	{
		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
		$inventions			= 	$_modelInvention->findBidInventions($_POST['FirmID']);

		if(is_array($inventions))
		{
			$this->purse($inventions,'inventions',0);
		}
	}


	/**
	 * 发布一个新发明
	 *
	 */
	function	actionAddInvention()
	{

		$invention["InventorID"]		=	$_POST["InventorID"];
		$invention["ClientID"]			=	$_POST["ClientID"];
		$invention['InventorID']		=	$_POST['InventorID'];
		$invention["Title"]				=	$_POST["Title"];
		$invention["KeyWords"]			=	$_POST["KeyWords"];
		$invention["Invented"]			=	date("Y-m-d", strtotime($_POST["Invented"]));
		$invention["Docket"]			=	$_POST["Docket"];
		$invention["PractitionerID"]	=	0;
		$invention["FirmID"]			=	0;
		$invention["Type"]				=	-1;
		$invention['Status']			=	0;
		$invention["Created"]			=	date("Y-m-d", time());
		$invention['Description']		=	htmlspecialchars($_POST['Description']);

		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
		$inventionID	=	$_modelInvention->addInvention($invention);
		if($inventionID > 0)
		{
			/**
			 * successfully disclose new invention
			 */
			if($_POST['FileNames'] != "")
			{
				$_modelAction	=&	FLEA::getSingleton('Model_Action');
				$_modelDescription	=&	FLEA::getSingleton('Model_Description');
				$action1['InventionID']	=	$inventionID;
				$action1['Type']		=	0;
				$action1['Date']		=	date("Y-m-d", time());
				$actionID				=	$_modelAction->create($action1);
					
				$files	=	split("-",$_POST['FileNames']);

				for($k=0; $k<count($files); $k++)
				{
					if($files[$k] != "")
					{
						$description['ActionID']	=	$actionID;
						$description['Date']		=	date("Y-m-d", time());
						$description['Drawing0']	=	"Drawing/".$files[$k];
						$_modelDescription->create($description);
					}
				}

				$invention1['InventionID']	=	$inventionID;
				$invention1['LastActionDate']=	$description['Date'];
				$_modelInvention->save($invention1);
			}
			$result	=	1;

		}
		else
		{
			/**
			 * program error
			 */
			$result	=	0;
		}

		$this->showResult($result);
	}
	/**
	 * publish invention 接口
	 *
	 */
	function	actionPublishInvention()
	{
		error_reporting(null);
		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
		$inventionID		=	$_POST['InventionID'];

		$invention['InventionID']	=	$inventionID;
		$invention['Status']		=	3;
		$invention['Published']		=	date("Y-m-d");

		/*
		 * add at 2009-4-1
		 */
		$date	=	getdate();
		$year		=	$date['year']+1;

		$date['year']	=	$year;
		
		$invention['ActionDueDate']	=	date("Y-m-d",mktime(0,0,0, $date['mon'],$date['mday'], $year));
		/**
		 *
		 */
		$result1	=	$_modelInvention->save($invention);

		if($result1 == 1 )
		{
			$invention	=	$_modelInvention->findInvention($inventionID);

			$filename 	= $invention['Title'].'_'.$invention['InventionID'].'.html';
			$handle 	= fopen('Inventions/'.$filename, 'w');
			$fp 		= fopen(url("Default","ShowInvention",array("InventionID"=>$invention['InventionID'])),'r');
			while (!feof($fp))
			{
				$buffer = fgets($fp, 4096);   //读的大小不能用filesize()不然会出错
				fwrite($handle, $buffer) ;
			}
			fclose($handle);
			fclose($fp);

				
		}
		$this->showResult($result1);

	}


	function 	actionUpdateInvention()
	{
		$invention	=	$_POST;

		if($_POST['Type'] != -1)
		{
			$invention['Type']			=	$_POST['Type'];
		}

		$invention['Status']		=	$_POST['Status'];
		if($invention['Status'] < 3)
		{
			$invention['Type']	=	$invention['Status']-1;
			$invention['Status']=	0;
		}
		if($invention['Status'] == 1)
		{
			$invention['PFDate']	=	date("Y-m-d",time());
		}
		else if($invention['Status'] == 9)
		{
			if($_POST['IssueDate'] != "")
			{
				$invention['IssueDate']	=	date("Y-m-d",strtotime($_POST['IssueDate']));

				$issueDate	=	getdate(strtotime($_POST['IssueDate']));
				$year		=	$issueDate['year']+3;
				$month		=	$issueDate['mon']+6;
				if($month>12)
				{
					$year	+=	1;
					$month	-=	12;
				}

				$issueDate['year']	=	$year;
				$issueDate['mon']	=	$month;
				$invention['RenewalDate']	=	date("Y-m-d",mktime(0,0,0, $month, $issueDate['mday'],$year));
				//add 2009-3-26
				$invention['ActionDueDate']	=	$invention['RenewalDate'];
			}
			else
			{
				$invention['IssueDate']	=	date("Y-m-d",time());
			}

		}

		if($_POST['BBid'] != -1)
		{
			$invention['BBid']			=	$_POST['BBid'];
		}


		$invention['MaxBudget']			=	$_POST['MaxBudget']==""?0:$_POST['MaxBudget'];

		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
		$result				=	$_modelInvention->save($invention);

		$this->showResult($result);
	}

	function 	actionUpdateBasicInvention()
	{
		$invention['InventionID']	=	$_POST['InventionID'];

		$invention['Title']			=	$_POST['Title'];
		$invention['Docket']		=	$_POST['Docket'];
		$invention['KeyWords']		=	$_POST['KeyWords'];
		$invention['Description']	=	$_POST['Description'];

		$role	=	$this->checkLogin();

		if($role==1)
		{
			$invention1	=	$this->_modelInvention->findInvention($invention['InventionID']);

			$result	=	$this->_modelInvention->save($invention);

		}
		else
		{
			$result	=	-2;
		}

		$this->function	=	"ShowInvention";
		$this->viewdata['invention']	=	$this->_modelInvention->findInvention($_POST['InventionID']);
		$this->viewdata['result']		=	$result;
		$this->show();
	}

	function 	actionSetMaxBudget()
	{
		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');

		$invention['InventionID']	=	$_POST['InventionID'];
		$invention['MaxBudget']		=	$_POST['MaxBudget'];

		$result	=	$_modelInvention->save($invention);

		$this->showResult($result);
	}

	function 	actionSaveFirmDocket()
	{
		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');

		$invention['InventionID']	=	$_POST['InventionID'];
		$invention['FirmDocket']	=	$_POST['FirmDocket'];

		$result	=	$_modelInvention->save($invention);

		$this->showResult($result);
	}


	function	actionAssignToFirmOther()
	{
		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');

		$invention['InventionID']	=	$_POST['InventionID'];
		$invention['FirmID']		=	$_POST['FirmID'];
		$invention['MaxBudget']		=	$_POST['MaxBudget'];
		$invention['TargetDate']	=	date("Y-m-d",strtotime($_POST['DeadlineDate']));
		$invention['ActionDueDate']	=	$invention['TargetDate'];
		$invention['BConfirm']		=	0;

		$result	=	$_modelInvention->save($invention);
		$this->showResult($result);
	}

	function	actionAcceptAssignInvention()
	{
	
		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
		$invention['InventionID']	=	$_POST['InventionID'];
		$invention['BConfirm']		=	$_POST['BConfirm'];
		$invention['FirmID']		=	$_POST['FirmID'];

		$result	=	$_modelInvention->save($invention);
		if($result == 1)
		{
			$result	=	$invention['BConfirm']+2;
		}
		
		if($invention['BConfirm'] == 1)
		{
			$_modelActionEvent	=&	FLEA::getSingleton("Model_ActionEvent");
			$_modelActionEvent->updateActionEvent($invention['FirmID'],$_POST['ClientID'],"Accept invention");
		}
		
		
		$this->showResult($result);
	}

	function 	actionAssignToPractitioner()
	{
		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
		$invention['InventionID']	=	$_POST['InventionID'];
		$invention['PractitionerID']=	$_POST['PractitionerID'];

		$result	=	$_modelInvention->save($invention);

		$this->showResult($result);
	}

	function	actionSpecifyFillingDueDate()
	{
		$invention['InventionID']	=	$_POST['InventionID'];
		$invention['TargetDate']	=	date("Y-m-d",strtotime($_POST['TargetDate']));

		$result		=	$this->_modelInvention->save($invention);

		$action1['Type']	=	2;
		$action1['DueDate']	=	$invention['TargetDate'];
		$action1['InventionID']	=	$_POST['InventionID'];
		$action1['Date']		=	date("Y-m-d",time());
		$result1	=	$this->_modelAction->create($action1);

		$result		=	$result && $result1;

		$this->function	=	"ShowInvention";
		$this->viewdata['invention']	=	$this->_modelInvention->findInvention($_POST['InventionID']);
		$this->viewdata['result']		=	$result;
		$this->show();

	}



	function 	actionUploadAction()
	{
		$uploaddir = 'Drawing/';
		$uploadfile = $uploaddir . basename($_FILES['Filedata']['name']);
		$temploadfile = $_FILES['Filedata']['tmp_name'];
		if(move_uploaded_file($temploadfile , $uploadfile))
		{
			print 1;
		}
		else
		{
			print 0;
		}
	}

	function	actionAddAction()
	{
		$_modelInvention=&	FLEA::getSingleton('Model_Invention');
		$_modelAction	=&	FLEA::getSingleton('Model_Action');
		$_modelDescription	=&	FLEA::getSingleton('Model_Description');

		$action1['Title']				=	$_POST['Title']!=''?$_POST['Title']:'';
		$action1['Type']				=	$_POST['Type'];
		$action1['Description']			=	$_POST['Description'];
		$action1['InventionID']			=	$_POST['InventionID'];
		$action1['Date']				=	date("Y-m-d", time());
		$action1['Cost']				=	$_POST['Cost'];

		$actionID	=	$_modelAction->create($action1);

		if($_POST['FileNames'] != "")
		{
			$files	=	split("-",$_POST['FileNames']);

			for($k=0; $k<count($files); $k++)
			{
				if($files[$k] != "")
				{
					$description['ActionID']	=	$actionID;
					$description['Date']		=	date("Y-m-d", time());
					$description['Drawing0']	=	"Drawing/".$files[$k];
					$_modelDescription->create($description);
				}
			}
		}
		$invention['InventionID']	=	$_POST['InventionID'];
		$invention['LastActionDate']=	$action1['Date'];
		$_modelInvention->save($invention);
		$this->showResult($actionID>0?1:0);
	}

	function	actionResponseToOffice()
	{
		$_modelInvention=&	FLEA::getSingleton('Model_Invention');
		$_modelAction	=&	FLEA::getSingleton('Model_Action');
		$_modelDescription	=&	FLEA::getSingleton('Model_Description');

		$action1['ResponseActionID']	=	$_POST['ResponseActionID'];
		$action1['OfficeActionType']	=	$_POST['Type'];
		$action1['Type']				=	3;
		$action1['Description']			=	$_POST['Description'];
		$action1['Date']				=	date("Y-m-d", time());
		$action1['Cost']				=	$_POST['Cost'];
		$action1['InventionID']			=	$_POST['InventionID'];
		$action1['ResponseDate']		=	$_POST['ResponseDate'];

		if($action1['OfficeActionType'] == 3)
		{
			$action2['Cost']		=	$_POST['Cost'];
			$action2['ActionID']	=	$action1['ResponseActionID'];
			$action2['OfficeActionMailed']	=	$action1['ResponseDate'];
				
			$today		=	getdate(strtotime($_POST['ResponseDate']));
			$month		=	$today['mon']+2;
			if($month>12)
			{
				$year	+=	1;
				$month	-=	12;
			}
			$action2['ResponseField']			=	$action1['ResponseDate'];
			$action2['OfficeActionResponse']	=	date("Y-m-d",mktime(0,0,0, $month, $today['mday'],$today['year']));
			//$action2['OfficeActionType']		=	3;
				
			$today		=	getdate(time());
			$month		=	$today['mon']+2;
			if($month>12)
			{
				$year	+=	1;
				$month	-=	12;
			}
			$action3['OfficeActionMailed']	=	date("Y-m-d",mktime(0,0,0, $month, $today['mday'],$today['year']));
			$action3['OfficeActionResponse']=	$action3['OfficeActionMailed'];
			$action3['ResponseType']		=	4;
			$action3['Type']				=	5;
			$action3['OfficeActionType']	=	-1;
			$action3['InventionID']			=	$_POST['InventionID'];
				
			$action3ID	=	$_modelAction->create($action3);
				
		}
		else
		{
			$action2['ActionID']	=	$action1['ResponseActionID'];
			$action2['ResponseType']=	$action1['OfficeActionType'];
			$action2['Cost']		=	$_POST['Cost'];
			$action2['ResponseField']			=	$action1['ResponseDate'];
		}

		$actionID	=	$_modelAction->create($action1);
		$_modelAction->save($action2);

		if($action2['OfficeActionResponse'] != "")
		{
			$invention['InventionID']	=	$_POST['InventionID'];
			$invention['ActionDueDate']	=	$action2['OfficeActionResponse'];
			$_modelInvention->save($invention);
		}

		if($_POST['FileNames'] != "")
		{
			$files	=	split("-",$_POST['FileNames']);

			for($k=0; $k<count($files); $k++)
			{
				if($files[$k] != "")
				{
					$description['ActionID']	=	$actionID;
					$description['Date']		=	date("Y-m-d", time());
					$description['Drawing0']	=	"Drawing/".$files[$k];
					$_modelDescription->create($description);
				}
			}
		}
		$this->showResult($actionID>0?1:0);
	}


	function	actionAddFieldAction()
	{
		
		
		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
		$_modelAction		=&	FLEA::getSingleton('Model_Action');
		$_modelDescription	=&	FLEA::getSingleton('Model_Description');

		$action1['Title']				=	$_POST['Title'];
		$action1['Type']				=	2;
		$action1['Description']			=	$_POST['Description'];
		$action1['InventionID']			=	$_POST['InventionID'];
		$action1['Date']				=	date("Y-m-d", time());
		$action1['Cost']				=	$_POST['Cost'];

		$action2	=	$_modelAction->find("Type=2 and InventionID=".$action1['InventionID']);
		if($action2['InventionID'] == $action1['InventionID'])
		{
			$action1['ActionID']	=	$action2['ActionID'];
			$_modelAction->save($action1);
			$actionID	=	$action1['ActionID'];
		}
		else
		{
			$actionID	=	$_modelAction->create($action1);
		}

		if($_POST['FileNames'] != "")
		{
			$files	=	split("-",$_POST['FileNames']);

			for($k=0; $k<count($files); $k++)
			{
				if($files[$k] != "")
				{
					$description['ActionID']	=	$actionID;
					$description['Date']		=	date("Y-m-d", time());
					$description['Drawing0']	=	"Drawing/".$files[$k];
					$_modelDescription->create($description);
				}
			}
		}

		$status	=	$_POST['Status'];
		
		if($status == 0)
		{
			$invention['InventionID']	=	$_POST['InventionID'];
			$invention['Status']		=	6;
			$invention["PFDate"]		=	date("Y-m-d", strtotime($_POST["FillingDate"]));
			$invention["UFDate"]		=	"";
			$invention['ApplicationNumber']=$_POST['SerialNumber'];
			//$invention['LastActionDate']=	$action1['Date'];
			//$invention['ActionDueDate']=	"";
			//add 2009-3-26
			$today		=	getdate(strtotime($_POST['FillingDate']));
			$year		=	$today['year']+1;
	
	
			$invention['ActionDueDate']	=	date("Y-m-d",mktime(0,0,0, $today['mon'], $today['mday'],$year));
	
			//
			$_modelInvention->save($invention);
		}
		else if($status == 1)
		{
			$invention['InventionID']	=	$_POST['InventionID'];
			$invention['Status']		=	6;
			$invention["UFDate"]		=	date("Y-m-d", strtotime($_POST["FillingDate"]));
			$invention['PFDate']		=	"";
			$invention['ApplicationNumber']=$_POST['SerialNumber'];
	
			$invention['ActionDueDate']	=	"";
	
			//
			$_modelInvention->save($invention);
		}
		else 
		{
			$invention['InventionID']	=	$_POST['InventionID'];
			$invention['Status']		=	6;
			$invention["UFDate"]		=	"";
			$invention["PFDate"]		=	"";
			$invention['ApplicationNumber']=$_POST['SerialNumber'];
	
			//
			$_modelInvention->save($invention);
		}
		

		$this->showResult($action1>0?1:0);
	}

	function	actionAddOfficeAction()
	{
		$_modelAction	=&	FLEA::getSingleton('Model_Action');
		$_modelDescription=&FLEA::getSingleton('Model_Description');
		$_modelInvention=&	FLEA::getSingleton('Model_Invention');

		$action1['Type']				=	5;
		$action1['InventionID']			=	$_POST['InventionID'];
		$action1['Date']				=	date("Y-m-d", time());
		$action1['OfficeActionMailed']	=	date("Y-m-d",strtotime($_POST['OfficeActionMailed']));
		$action1['OfficeActionType']	=	$_POST['OfficeActionType'];
		$action1['OfficeActionResponse']=	date("Y-m-d",strtotime($_POST['OfficeActionResponse']));
		$action1['Comments']			=	$_POST['Comments'];
		if($action1['OfficeActionType'] == 4)
		{
			$action1['ResponseType']	=	3;
		}
		else
		{
			$action1['ResponseType']		=	0;
		}


		$action1['ExpectedBudget']				=	$_POST['Cost'];
		//addd at 2009-03-20
		$action2['ResponseField']			=	date("Y-m-d", time());
		$today		=	getdate(strtotime($_POST['OfficeActionMailed']));
		$month		=	$today['mon']+6;
		if($month>12)
		{
			$year	+=	1;
			$month	-=	12;
		}

		$action1['GoesAbandoned']	=	date("Y-m-d",mktime(0,0,0, $month, $today['mday'],$today['year']));


		$actionID	=	$_modelAction->create($action1);

		if($_POST['FileNames'] != "")
		{
			$files	=	split("-",$_POST['FileNames']);

			for($k=0; $k<count($files); $k++)
			{
				if($files[$k] != "")
				{
					$description['ActionID']	=	$actionID;
					$description['Date']		=	date("Y-m-d", time());
					$description['Drawing0']	=	"Drawing/".$files[$k];
					$_modelDescription->create($description);
				}
			}
		}

		$invention['InventionID']	=	$_POST['InventionID'];
		$invention['GoesAbandoned']	=	$action1['GoesAbandoned'];
		$invention['LastActionDate']=	$action1['Date'];
		$invention['ActionDueDate']	=	$_POST['ResponseDueDate'];
		$invention['TargetDate']	=	$action1['OfficeActionResponse'];
		$_modelInvention->save($invention);

		$result	=	1;
		$this->showResult($result);

	}

	function	actionGetOfficeActions()
	{


	}

	function	actionSearchInventions()
	{

		if($_POST['KeyWords'] != "")
		{
			$keyWords	=	$_POST['KeyWords'];
		}
		else
		{
			$keyWords	=	$_GET['KeyWords'];
		}

		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');

		$inventions	=	$_modelInvention->searchInventions($keyWords);dump($inventions);
		$this->function	=	"InventionsList";
		$this->viewdata['inventions']	=	$inventions;
		$this->viewdata['KeyWords']		=	$keyWords;
		$this->_executeView(View_DIR.DS.'SearchInventions.php',$this->viewdata);
	}

	function	actionSetBudget()
	{
		$_modelBid	=&	FLEA::getSingleton('Model_Bid');

		$bid		=	$_POST;

		$result		=	$_modelBid->saveBid($bid);
		$this->showResult($result);
	}

	function	actionGetBudget()
	{
		$inventionID	=	$_POST['InventionID'];

		$role	=	$this->checkLogin();

		if($role == 3)
		{
			$firmID	=	$this->viewdata['user']['firm']['FirmID'];

			$_modelBid	=&	FLEA::getSingleton('Model_Bid');
			$bid		=	$_modelBid->findBid($inventionID,$firmID);
			$result		=	$bid['Budget']==""?-1:$bid['Budget'];
		}
		else
		{
			$result		=	-2;
		}
		$this->showResult($result);
	}

	function	actionGetValue()
	{
		$inventionID	=	$_POST['InventionID'];

		$_modelAuction	=&	FLEA::getSingleton('Model_Auction');
		$auction		=	$_modelAuction->findAuction($inventionID);

		if(is_array($auction))
		{
			$this->purse($auction,'auction',0);
		}
	}

	function	actionSetValue()
	{
		$auction	=	$_POST;
		//dump($_POST);
		$_modelAuction	=&	FLEA::getSingleton('Model_Auction');
		$result			=	$_modelAuction->saveAuction($auction);
		$this->showResult($result>0?1:0);
	}

	function	actionGetInventionBudgets()
	{
		$inventionID	=	$_POST['InventionID'];

		$_modelBid		=&	FLEA::getSingleton('Model_Bid');
		$budgets		=	$_modelBid->findAllBudgets($inventionID);

		if(is_array($budgets))
		{
			$this->purse($budgets,'budgets',0);
		}
	}

	function	actionRenewalFee()
	{
		$_modelRenewal	=&	FLEA::getSingleton('Model_Renewal');
		$renewal['InventionID']	=	$_POST['InventionID'];
		$renewal['FirmID']		=	$_POST['FirmID'];

		$renewal["Date"]		=	date("Y-m-d", strtotime($_POST["RenewalDate"]));
		$renewal['ReferenceNumber']=$_POST['ReferenceNumber'];

		$result	=	$_modelRenewal->create($renewal);
		$this->showResult($result>0?1:0);
	}

	function	actionGetTrackInventions()
	{
		$clientID	=	$_POST['ClientID'];
		$clientID	=	$clientID == '' ? 0:$clientID;

		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');

		$today				=	getdate();
		$year				=	getdate(mktime(0,0,0,1,1,$today['year']));
		$nextYear			=	getdate(mktime(0,0,0,1,1,$today['year']-1));

		//$inventions			=	$_modelInvention->findTrackInventions($clientID,$year['year'],$nextYear['year']);
		$inventions			=	$_modelInvention->findTracks($clientID,$year['year'],$nextYear['year']);
		if(is_array($inventions))
		{
			$this->purse($inventions,'inventions',0);
		}
	}

	function	actionGetTrackInventionsByFirm()
	{
		$clientID	=	$_POST['ClientID'];
		$clientID	=	$clientID == '' ? 0:$clientID;

		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');

		$inventions			=	$_modelInvention->findTrackInventionsByFirm($clientID);
		if(is_array($inventions))
		{
			$this->purse($inventions,'inventions',0);
		}
	}


	function	actionRecordTradeSecret()
	{
		$_modelTradeSecret	=&	FLEA::getSingleton('Model_TradeSecret');
		$trade				=	$_POST;
		$trade['Invented']	=	date("Y-m-d", time());

		$tradeID			=	$_modelTradeSecret->create($trade);

		$tradeID			=	$tradeID>0?1:0;
		$this->showResult($tradeID);
	}

	function	actionGetTradesInformation()
	{
		$clientID		=	$_POST['ClientID'];
		$clientID		=	$clientID==''?0:$clientID;

		$_modelTradeSecret	=&	FLEA::getSingleton('Model_TradeSecret');
		$_modelTradeMark	=&	FLEA::getSingleton('Model_TradeMark');


		$tradeSecrets		=	$_modelTradeSecret->findAll('ClientID='.$clientID);
		$tradesMarks		=	$_modelTradeMark->findTradeMarks($clientID);



		print '<invention>';
		if(is_array($tradesMarks))
		{
			$this->purse($tradesMarks,'tradeMarks',0);
		}
		if(is_array($tradeSecrets))
		{
			$this->purse($tradeSecrets,'tradeSecrets',0);
		}
		print '</invention>';
	}

	function	actionProtectAsSecret()
	{
		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
		$_modelTradeSecret	=&	FLEA::getSingleton('Model_TradeSecret');

		$inventionID		=	$_POST['InventionID'];
		$inventionID		=	$inventionID==''?0:$inventionID;


		$clientID				=	$_POST['ClientID'];
		$clientID			=	$clientID==''?0:$clientID;

		$invention	=	$_modelInvention->findByField('InventionID',$inventionID);

		$secret['TradeSecret']	=	$invention['Title'];
		$secret['ClientID']		=	$clientID;
		$secret['Invented']	=	date("Y-m-d", time());
		$_modelInvention->removeByPkv($inventionID);
		$result	=	$_modelTradeSecret->create($secret);

		$this->showResult($result>0?1:0);
	}
}
?>